//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.huashi.otg.sdk;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HsOtgApi {
	HsUsbHepler usbHepler;
	Context instance;
	Handler handler;
	boolean init_suss = false;
	private String path;
	private final BroadcastReceiver mUsbReceiver1 = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			Log.e("usb_permission  111", action);
			if ("android.hardware.usb.action.USB_DEVICE_DETACHED".equals(action)) {
				UsbDevice msg = (UsbDevice) intent.getParcelableExtra("device");
				String deviceName = msg.getDeviceName();
				if (msg != null && msg.equals(deviceName)) {
					Message msg1 = new Message();
					if (HsOtgApi.this.init() != 1) {
						msg1.what = 99;
						msg1.obj = "USB设备拔出，应用程序即将关闭。";
						HsOtgApi.this.handler.sendMessage(msg1);
						HsOtgApi.this.init_suss = false;
						HsOtgApi.this.usbHepler = null;
					}
				}
			} else if ("com.android.USB_PERMISSION".equals(action)) {
				Message msg2 = new Message();
				msg2.what = 100;
				msg2.obj = "USB设备无权限";
				HsOtgApi.this.handler.sendMessage(msg2);
			}

		}
	};
	private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			Log.e("usb_permission  000", action);
			if ("com.android.USB_PERMISSION".equals(action)) {
				Message msg = Message.obtain();
				synchronized (this) {
					UsbDevice device = (UsbDevice) intent.getParcelableExtra("device");
					if (intent.getBooleanExtra("permission", false)) {
						if (device != null) {
							Log.e("mUsbReceiver", device + "有权限");
							if (HsOtgApi.this.usbHepler == null) {
								try {
									HsOtgApi.this.usbHepler = new HsUsbHepler(HsOtgApi.this.instance);
								} catch (Exception var8) {
									var8.printStackTrace();
								}
							}

							HsOtgApi.this.init_suss = true;
							msg.what = 1;
							msg.obj = "USB permission success";
							HsOtgApi.this.handler.sendMessage(msg);
						}
					} else {
						String con = "permission denied for device 没有权限" + device;
						HsOtgApi.this.init_suss = false;
						msg.what = -99;
						msg.obj = con;
						HsOtgApi.this.handler.sendMessage(msg);
					}
				}
			}

		}
	};

	public HsOtgApi(Handler handler, Context instance) {
		this.handler = handler;
		this.instance = instance;
	}

	/**
	 * 初始化
	 * 
	 * @return 返回值：1：成功 2：设备未授权 3：未找到设备 -1：未知错误
	 */
	public int init() {
		Message var1 = Message.obtain();

		try {
			IntentFilter e = new IntentFilter();
			e.addAction("com.android.USB_PERMISSION");
			this.instance.registerReceiver(this.mUsbReceiver, e);
			IntentFilter e1 = new IntentFilter();
			e1.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED");
			this.instance.registerReceiver(this.mUsbReceiver1, e1);
			this.usbHepler = new HsUsbHepler(this.instance);
		} catch (Exception var4) {
			if (var4.getCause() == null) {
				return -1;
			}

			this.init_suss = false;
			return -1;
		}

		this.init_suss = true;
		return 1;
	}

	public void unInit() {
		this.instance.unregisterReceiver(this.mUsbReceiver);
		this.instance.unregisterReceiver(this.mUsbReceiver1);
	}

	public String GetSAMID() {
		String sam = "";
		return !this.init_suss ? sam : this.usbHepler.getSAMID();
	}

	/**
	 * 卡认证
	 * 
	 * @param sendTime 发送指令等待时间
	 * @param recvTime 接收指令等待时间
	 * @return 1：成功 0：失败
	 */
	public int Authenticate(long sendTime, long recvTime) {
		return !this.init_suss ? 0
				: (this.usbHepler.FindCard(sendTime, recvTime) == 1
						&& this.usbHepler.SelectCard(sendTime, recvTime) == 1 ? 1 : 2);
	}

	/**
	 * 卡认证(只下发指令，不判断返回值，此函数用于不拿起身份证进行多次读卡)
	 * 
	 * @param sendTime 发送指令等待时间
	 * @param recvTime 接收指令等待时间
	 * @return 1：成功 0：失败
	 */
	public int NotAuthenticate(long sendTime, long recvTime) {
		if (!this.init_suss) {
			return 0;
		} else {
			this.usbHepler.FindCard(sendTime, recvTime);
			this.usbHepler.SelectCard(sendTime, recvTime);
			return 1;
		}
	}

	/**
	 * 读身份证
	 * 
	 * @param ici      身份证信息
	 * @param sendTime 发送指令等待时间
	 * @param recvTime 接收指令等待时间
	 * @return 返回值： 0：读卡失败 1：读卡成功 2：通讯有误
	 */
	public int ReadCard(HSIDCardInfo ici, long sendTime, long recvTime) {
		try {
			if (!this.init_suss) {
				return 0;
			} else {
				byte[] e = new byte[4096];
				if (this.usbHepler.ReadCard(e, sendTime, recvTime) != 1) {
					return 2;
				} else {
					byte[] headByte = new byte[256];
					byte[] wltData = new byte[1024];
					byte[] pucFPMsg = new byte[1024];
					System.arraycopy(e, 16, headByte, 0, headByte.length);
					System.arraycopy(e, 272, wltData, 0, wltData.length);
					System.arraycopy(e, 1296, pucFPMsg, 0, pucFPMsg.length);
					ici.setwltdata(wltData);
					ici.setFpDate(pucFPMsg);
					Utility.PersonInfoUtoG(headByte, ici);
					return 1;
				}
			}
		} catch (Exception var10) {
			return 2;
		}
	}
}
